package com.lucker.service.sms;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.stereotype.Service;

import com.lucker.common.util.HttpUtil;
import com.lucker.service.model.Sms;

@Service
public class SmsServiceImpl implements SmsService {
	
	private final String url = "http://www.jianzhou.sh.cn/JianzhouSMSWSServer/http/sendBatchMessage";
	private final String account = "sdk_xiaozhuang";
	private final String pwd = "hvf2v8xh";
	private final String postfix = "【小铺来了】";
		
	private ExecutorService	executorService	= Executors.newFixedThreadPool(10);
	public void sendSms(Sms sms) {
		String _sms_content = sms.getSmsTextContant() + sms.getPostfix();
		String _mobile = sms.getMobile();
		if(sms.isAsyn) {
			//这个异步方法后面用消息队列实现，效率更高
			syncSendSms(sms);
		}else {
			sendSms(_mobile,_sms_content);
		}
	}
	
	public void syncSendSms(final Sms sms) {
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				sms.setAsyn(false);
				sendSms(sms);
			}
		});
	}
	/***
	 * 同步发送短信方法
	 * @param mobile
	 * @param contant
	 * @return
	 */
	
	private String sendSms(String mobile, String content) {
		
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("account", account);
		paramMap.put("password", pwd);
		paramMap.put("destmobile", mobile);
		paramMap.put("msgText",  content);
		paramMap.put("sendDateTime", "");
		try {
			return HttpUtil.post(url, paramMap);
		}catch(Exception e) {
			return null;
		}
	}
	
	public static void main(String args[]) {
		Sms s = new Sms();
		s.setMobile("13598008451");
		//s.setSmsTextContant("您在小铺来了申请提现到 中国工商银行 9558******1111 张三三，请核实是否本人操作，如有问题请及时联系小铺客服4005122 ");
		s.setSmsTextContant("3333333333dfsdgsfdgsf ");
		s.setPostfix("【小铺来了】");
		s.setAsyn(false);
		SmsServiceImpl ser = new SmsServiceImpl();
		ser.sendSms(s);
	}
	
}
